package com.platform.module.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.core.domain.Ztree;
import com.platform.common.core.text.Convert;
import com.platform.common.utils.DateUtils;
import com.platform.common.utils.StringUtils;
import com.platform.module.domain.*;
import com.platform.module.mapper.*;
import com.platform.module.service.IBaseSubscriberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 地址资料Service业务层处理
 * 
 * @author wangqi
 * @date 2020-06-24
 */
@Service
public class BaseSubscriberServiceImpl extends ServiceImpl<BaseSubscriberMapper,BaseSubscriber> implements IBaseSubscriberService
{
    @Autowired
    private BaseSubscriberMapper baseSubscriberMapper;

    @Autowired
    private LinkSubMeterMapper linkSubMeterMapper;

    @Autowired
    private LinkSubConcentratorMapper linkSubConcentratorMapper;

    @Autowired
    private LinkSubValveMapper linkSubValveMapper;

    @Autowired
    private LinkSubTempMapper linkSubTempMapper;

    /**
     * 查询地址资料列表
     * 
     * @param baseSubscriber 地址资料
     * @return 地址资料
     */
    @Override
    public List<BaseSubscriber> selectBaseSubscriberList(BaseSubscriber baseSubscriber)
    {
        return baseSubscriberMapper.selectBaseSubscriberList(baseSubscriber);
    }

    /**
     * 新增地址资料
     * 
     * @param baseSubscriber 地址资料
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBaseSubscriber(BaseSubscriber baseSubscriber)
    {

        BaseSubscriber info = baseSubscriberMapper.selectById(baseSubscriber.getParentId());
        baseSubscriber.setAncestors(info.getAncestors() + "," + baseSubscriber.getParentId());
        baseSubscriber.setCreateTime(DateUtils.getNowDate());
        return baseSubscriberMapper.insert(baseSubscriber);
    }

    /**
     * 修改地址资料
     * 
     * @param baseSubscriber 地址资料
     * @return 结果
     */
    @Override
    @Transactional
    public int updateBaseSubscriber(BaseSubscriber baseSubscriber)
    {

        BaseSubscriber newParentSubscriber = baseSubscriberMapper.selectById(baseSubscriber.getParentId());
        baseSubscriber.setUpdateTime(DateUtils.getNowDate());
        BaseSubscriber oldSubscriber = baseSubscriberMapper.selectById(baseSubscriber.getSubscriberId());
        if (StringUtils.isNotNull(newParentSubscriber) && StringUtils.isNotNull(oldSubscriber))
        {
            String newAncestors = newParentSubscriber.getAncestors() + "," + newParentSubscriber.getSubscriberId();
            String oldAncestors = oldSubscriber.getAncestors();
            baseSubscriber.setAncestors(newAncestors);
            updateSubscriberChildren(baseSubscriber.getSubscriberId(), newAncestors, oldAncestors);
        }
        int result = baseSubscriberMapper.updateById(baseSubscriber);
        return result;
    }

    /**
     * 修改子元素关系
     *
     * @param subscriberId 被修改的部门ID
     * @param newAncestors 新的父ID集合
     * @param oldAncestors 旧的父ID集合
     */
    public void updateSubscriberChildren(Long subscriberId, String newAncestors, String oldAncestors)
    {
        List<BaseSubscriber> children = baseSubscriberMapper.selectChildrenSubscriberById(subscriberId);
        for (BaseSubscriber child : children)
        {
            child.setAncestors(child.getAncestors().replace(oldAncestors, newAncestors));
        }
        if (children.size() > 0)
        {
            baseSubscriberMapper.updateSubscriberChildren(children);
        }
    }

    /**
     * 删除地址资料对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteBaseSubscriberByIds(String ids)
    {
        return baseSubscriberMapper.deleteBatchIds(Arrays.asList(Convert.toStrArray(ids)));
    }

    /**
     * 删除地址资料信息
     * 
     * @param subscriberId 地址资料ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteBaseSubscriberById(Long subscriberId)
    {
        return baseSubscriberMapper.deleteById(subscriberId);
    }

    /**
     * 查询地址资料树列表
     * 
     * @return 所有地址资料信息
     */
    @Override
    public List<Ztree> selectBaseSubscriberTree()
    {
        List<BaseSubscriber> baseSubscriberList = baseSubscriberMapper.selectBaseSubscriberList(new BaseSubscriber());
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (BaseSubscriber baseSubscriber : baseSubscriberList)
        {
            Ztree ztree = new Ztree();
            ztree.setId(baseSubscriber.getSubscriberId());
            ztree.setpId(baseSubscriber.getParentId());
            ztree.setName(baseSubscriber.getSubscriberShort());
            ztree.setTitle(baseSubscriber.getSubscriberShort());
            ztrees.add(ztree);
        }
        return ztrees;
    }

    /**
     * 取消授权阀门地址
     *
     * @param subValve 阀门和地址关联信息
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteAuthSubValve(LinkSubValve subValve)
    {
        return linkSubValveMapper.delete(new QueryWrapper<LinkSubValve>().eq("valve_id",subValve.getValveId()).eq("subscriber_id",subValve.getSubscriberId()));
    }

    /**
     * 批量取消授权阀门地址
     *
     * @param subscriberId 地址ID
     * @param valveIds 需要删除的阀门数据ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteAuthSubValves(String subscriberId, String valveIds)
    {
        return linkSubValveMapper.delete(new QueryWrapper<LinkSubValve>().eq("subscriber_id",subscriberId).in("valve_id",Arrays.asList(Convert.toStrArray(valveIds))));
    }

    /**
     * 批量选择授权阀门地址
     *
     * @param subscriberId 地址ID
     * @param valveIds 需要删除的阀门数据ID
     * @return 结果
     */
    @Override
    @Transactional
    public int insertAuthSubValves(String subscriberId, String valveIds)
    {
        String[] valves = Convert.toStrArray(valveIds);
        // 新增阀门与地址管理
        List<LinkSubValve> list = new ArrayList<LinkSubValve>();
        for (String valveId : valves)
        {
            LinkSubValve ur = new LinkSubValve();
            ur.setValveId(valveId);
            ur.setSubscriberId(subscriberId);
            list.add(ur);
        }
        return linkSubValveMapper.batchSubValve(list);
    }

    /**
     * 取消授权热表地址
     *
     * @param subMeter 热表和地址关联信息
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteAuthSubMeter(LinkSubMeter subMeter)
    {
        return linkSubMeterMapper.delete(new QueryWrapper<LinkSubMeter>().eq("calorimeter_id",subMeter.getCalorimeterId()).eq("subscriber_id",subMeter.getSubscriberId()));
    }

    /**
     * 批量取消授权热表地址
     *
     * @param subscriberId 地址ID
     * @param calorimeterIds 需要删除的热表数据ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteAuthSubMeters(String subscriberId, String calorimeterIds)
    {
        return linkSubMeterMapper.delete(new QueryWrapper<LinkSubMeter>().eq("subscriber_id",subscriberId).in("calorimeter_id",Arrays.asList(Convert.toStrArray(calorimeterIds))));
    }

    /**
     * 批量选择授权热表地址
     *
     * @param subscriberId 地址ID
     * @param calorimeterIds 需要删除的热表数据ID
     * @return 结果
     */
    @Override
    @Transactional
    public int insertAuthSubMeters(String subscriberId, String calorimeterIds)
    {
        String[] calorimeters = Convert.toStrArray(calorimeterIds);
        // 新增热表与地址管理
        List<LinkSubMeter> list = new ArrayList<LinkSubMeter>();
        for (String calorimeterId : calorimeters)
        {
            LinkSubMeter ur = new LinkSubMeter();
            ur.setCalorimeterId(calorimeterId);
            ur.setSubscriberId(subscriberId);
            list.add(ur);
        }
        return linkSubMeterMapper.batchSubMeter(list);
    }

    /**
     * 取消授权热表地址
     *
     * @param subTemp 热表和地址关联信息
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteAuthSubTemp(LinkSubTemp subTemp)
    {
        return linkSubTempMapper.delete(new QueryWrapper<LinkSubTemp>().eq("brt_id",subTemp.getBrtId()).eq("subscriber_id",subTemp.getSubscriberId()));
    }

    /**
     * 批量取消授权热表地址
     *
     * @param subscriberId 地址ID
     * @param brtIds 需要删除的热表数据ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteAuthSubTemps(String subscriberId, String brtIds)
    {
        return linkSubTempMapper.delete(new QueryWrapper<LinkSubTemp>().eq("subscriber_id",subscriberId).in("brt_id",Arrays.asList(Convert.toStrArray(brtIds))));
    }

    /**
     * 批量选择授权热表地址
     *
     * @param subscriberId 地址ID
     * @param brtIds 需要删除的热表数据ID
     * @return 结果
     */
    @Override
    @Transactional
    public int insertAuthSubTemps(String subscriberId, String brtIds)
    {
        String[] brts = Convert.toStrArray(brtIds);
        // 新增热表与地址管理
        List<LinkSubTemp> list = new ArrayList<LinkSubTemp>();
        for (String brtId : brts)
        {
            LinkSubTemp ur = new LinkSubTemp();
            ur.setBrtId(brtId);
            ur.setSubscriberId(subscriberId);
            list.add(ur);
        }
        return linkSubTempMapper.batchSubTemp(list);
    }

    /**
     * 取消授权集中器地址
     *
     * @param subConcentrator 集中器和地址关联信息
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteAuthSubConcentrator(LinkSubConcentrator subConcentrator)
    {
        return linkSubConcentratorMapper.delete(new QueryWrapper<LinkSubConcentrator>().eq("concentrator_id",subConcentrator.getConcentratorId()).eq("subscriber_id",subConcentrator.getSubscriberId()));
    }

    /**
     * 批量取消授权集中器地址
     *
     * @param subscriberId 地址ID
     * @param concentratorIds 需要删除的集中器数据ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteAuthSubConcentrators(String subscriberId, String concentratorIds)
    {
        return linkSubConcentratorMapper.delete(new QueryWrapper<LinkSubConcentrator>().eq("subscriber_id",subscriberId).in("concentrator_id",Arrays.asList(Convert.toStrArray(concentratorIds))));
    }

    /**
     * 批量选择授权集中器地址
     *
     * @param subscriberId 地址ID
     * @param concentratorIds 需要删除的集中器数据ID
     * @return 结果
     */
    @Override
    @Transactional
    public int insertAuthSubConcentrators(String subscriberId, String concentratorIds)
    {
        String[] concentrators = Convert.toStrArray(concentratorIds);
        // 新增集中器与地址管理
        List<LinkSubConcentrator> list = new ArrayList<LinkSubConcentrator>();
        for (String concentratorId : concentrators)
        {
            LinkSubConcentrator ur = new LinkSubConcentrator();
            ur.setConcentratorId(concentratorId);
            ur.setSubscriberId(subscriberId);
            list.add(ur);
        }
        return linkSubConcentratorMapper.batchSubConcentrator(list);
    }


}
